/**
 * 
 */
package com.example.springbootoauth2.config.security;

import java.util.List;

import com.example.springbootoauth2.dao.UserDao;
import com.example.springbootoauth2.model.user.pojo.Role;
import com.example.springbootoauth2.model.user.pojo.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


/**
 * @Author Firmye
 * @Date 2017年12月7日 上午12:33:55
 * 
 * @Description 2.自定义UserDetailsService
 */
@Component
public class CustomUserDetailsService implements UserDetailsService {

	protected Log log = LogFactory.getLog(this.getClass());

	@Autowired
	private UserDao userDao;

	public CustomUserDetailsService() {
		super();
	}

	/**
	 * @Author Firmye
	 * @Date 2017年12月7日 上午12:34:37
	 *
	 * @Description 加载用户信息
	 */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userDao.queryUserByUsername(username);
		if (null == user) {
			throw new UsernameNotFoundException("---------- UserName " + username + " not found ----------");
		}

		log.info("---------- User:" + user + " ----------");

		List<Role> roles = userDao.queryRolesByUserId(user.getUserId());

		return new CustomUserDetails(user, roles);
	}

}
